package com.bj58.ecdata.dashboard.databll;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.TreeMap;

import net.sf.json.JSONObject;

import com.bj58.ecdata.dashboard.cache.ServiceBeanFactory;
import com.bj58.ecdata.dashboard.constant.IncomeCustomerConst;
import com.bj58.ecdata.dashboard.dbentity.CustomerBalances;
import com.bj58.ecdata.dashboard.dbentity.CustomerCashcircle;
import com.bj58.ecdata.dashboard.dbentity.CustomerCostBalance;
import com.bj58.ecdata.dashboard.dbentity.CustomerCostTransfer;
import com.bj58.ecdata.dashboard.dbentity.CustomerDispcost;
import com.bj58.ecdata.dashboard.dbentity.CustomerNewAdd;
import com.bj58.ecdata.dashboard.dbentity.CustomerTransfer;
import com.bj58.ecdata.dashboard.service.AbstractBaseService;
import com.bj58.ecdata.dashboard.utils.ReflectUtil;

public class CustomerBLL {
	private static AbstractBaseService<CustomerBalances> icmCustomerService = ServiceBeanFactory.get(CustomerBalances.class, null);
	private static AbstractBaseService<CustomerNewAdd> icmCustomerNewService = ServiceBeanFactory.get(CustomerNewAdd.class, null);
	private static AbstractBaseService<CustomerDispcost> icmCustomerDispcostService = ServiceBeanFactory.get(CustomerDispcost.class, null);
	private static AbstractBaseService<CustomerTransfer> icmCustomerTransferService = ServiceBeanFactory.get(CustomerTransfer.class, null);
	private static AbstractBaseService<CustomerCostBalance> icmCustomerCostBalanceService = ServiceBeanFactory.get(CustomerCostBalance.class, null);
	private static AbstractBaseService<CustomerCostTransfer> icmCustomerCostTransferService = ServiceBeanFactory
			.get(CustomerCostTransfer.class, null);
	private static AbstractBaseService<CustomerCashcircle> icmCustomerCashcircle = ServiceBeanFactory.get(CustomerCashcircle.class, null);
	private static final String paten_geneal = "stat_date='%s' and business_type='%s' and user_type='0' and stat_type='day'";
	private static final String paten_geneal_Cusbalance = "'%s'>=start_date and '%s'<=end_date and business_type='%s' LIMIT 1";
	private static final String paten_geneal_Custransfer = "'%s'>=start_date and '%s'<=end_date and business_type='%s' LIMIT 1";
	private static final String paten_geneal_Cashcircle = "'%s'>=start_date and '%s'<=end_date and business_type='%s' and user_type='1'  LIMIT 1";

	/**
	 * 获取客户余额>0等客户数趋势图数据
	 */
	public static String getCustomerNumJson(String dateGroup, String stat_type, String businessName, String customerType) {
		String fields[] = { "user_num", "balance_0", "balance_50", "cash_balance_0", "cash_balance_50", "jingzhun_num", "zhineng_num", "zhiding_num",
				"jingzhun_num_per", "zhineng_num_per", "zhiding_num_per" };
		if ("week_num".equals(dateGroup)) {
			dateGroup = "stat_date";
		}
		String cols = IncomeCustomerConst.getCustomerProCol(dateGroup);
		/*
		 * if("stat_date as week_begin".equals(dateGroup)) { dateGroup="week_begin"; }
		 */
		String condition = IncomeCustomerConst.getCustomerProCon(dateGroup, stat_type, businessName, customerType);
		List<CustomerBalances> list = icmCustomerService.getListByConditon(cols, condition, dateGroup);
		TreeMap<String, List<CustomerBalances>> stat_dateMap = new TreeMap<String, List<CustomerBalances>>(ReflectUtil.groupByField(list, dateGroup));

		JSONObject ob = new JSONObject();
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			CustomerBalances cityVo = stat_dateMap.get(date).get(0);
			for (String f : fields) {
				List<Number> curList = dataMap.get(f);
				if (curList == null) {
					curList = new ArrayList<Number>();
				}
				curList.add((Number) ReflectUtil.getVal(cityVo, f));
				dataMap.put(f, curList);
			}
		}
		ob.element("time", stat_dateMap.keySet());

		for (String p : dataMap.keySet()) {
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}

	/**
	 * 获取开通客户百分比数据
	 */
	public static String getCustomerNumJsonForPer(String stat_date, String stat_type, String businessName, String customerType) {
		String fields[] = { "jingzhun_num_per", "zhineng_num_per", "zhiding_num_per" };
		String cols = "jingzhun_num_per,zhineng_num_per,zhiding_num_per";
		String condition = " business_type='" + businessName + "' and stat_type='" + stat_type + "' and stat_date='" + stat_date
				+ "' and user_type='" + customerType + "'";
		List<CustomerBalances> list = icmCustomerService.getListByConditon(cols, condition, null);
		JSONObject ob = new JSONObject();
		CustomerBalances cityVo = list.get(0);
		for (String f : fields) {
			Number curList = (Number) ReflectUtil.getVal(cityVo, f);
			ob.element(f, curList);
		}
		return ob.toString();
	}

	/**
	 * 获取分类客户等客户数趋势图数据
	 */
	public static String getCustomerNumJsonForIndex(String dateGroup, String stat_type, String businessName, String customerIndex) {
		String fields[] = { "jingzhun_num", "zhineng_num", "zhiding_num" };
		String fields1[] = { "jingzhun_num_per", "zhineng_num_per", "zhiding_num_per" };
		if ("week_begin".equals(dateGroup)) {
			dateGroup = "stat_date";
		}
		String cols = IncomeCustomerConst.getCustomerProCol1(dateGroup);
		String condition = IncomeCustomerConst.getCustomerProCon1(dateGroup, stat_type, businessName, customerIndex);
		List<CustomerBalances> list = icmCustomerService.getListByConditon(cols, condition, dateGroup);
		TreeMap<String, List<CustomerBalances>> stat_dateMap = new TreeMap<String, List<CustomerBalances>>(ReflectUtil.groupByField(list, dateGroup));

		JSONObject ob = new JSONObject();
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {// 获得每日的日期

			list = stat_dateMap.get(date);
			for (int i = 0; i < list.size(); i++) {
				CustomerBalances cityVo = list.get(i);
				String type = (String) ReflectUtil.getVal(cityVo, "user_type");

				List<Number> curList = dataMap.get(type);
				if (curList == null) {
					curList = new ArrayList<Number>();
				}
				curList.add((Number) ReflectUtil.getVal(cityVo, customerIndex));
				dataMap.put(type, curList);
				/*
				 * if("all".equals(type)) {
				 */
				for (String f : fields) {
					List<Number> curList2 = dataMap.get(type + "#" + f);
					if (curList2 == null) {
						curList2 = new ArrayList<Number>();
					}
					curList2.add((Number) ReflectUtil.getVal(cityVo, f));
					dataMap.put(type + "#" + f, curList2);
				}
				for (String f : fields1) {
					List<Number> curList2 = dataMap.get("per" + type + f);
					if (curList2 == null) {
						curList2 = new ArrayList<Number>();
					}
					curList2.add((Number) ReflectUtil.getVal(cityVo, f));
					dataMap.put("per" + type + f, curList2);
				}
				/* } */
			}
		}
		ob.element("time", stat_dateMap.keySet());

		for (String p : dataMap.keySet()) {
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}

	/**
	 * 获取分类客户等漏斗图数据
	 */
	public static String getCustomerNumJsonForFunnel(String dateGroup, String dateVal, String businessName) {
		String fields[] = { "user_num", "balance_0", "balance_50", "cash_balance_0", "cash_balance_50" };
		CustomerBalances cityVo = null;
		String cols = "";
		String daytype = "";
		String condition = "";
		if ("".equals(dateVal)) {
			dateVal = icmCustomerService.getMaxDate();
		}
		if (dateGroup.equals("qtr_num")) {
			daytype = "week";
		} else if (dateGroup.equals("month_num")) {
			daytype = "month";
		} else {
			daytype = "day";
		}
		// 查询条件,获得周数或者月数
		String condition1 = String.format(paten_geneal, dateVal, businessName);
		// 查询结果
		List<CustomerBalances> listresult = icmCustomerService.getListByConditon(null, condition1, null);
		if (listresult.size() > 0) {
			cityVo = listresult.get(0);
		}
		String daytypenum = (String) ReflectUtil.getVal(cityVo, dateGroup);
		cols = IncomeCustomerConst.getCustomerProForFunnel(dateGroup);
		condition = IncomeCustomerConst.getCustomerProForFunnel(dateGroup, daytypenum, businessName, daytype);
		List<CustomerBalances> list = icmCustomerService.getListByConditon(cols, condition, dateGroup);
		JSONObject ob = new JSONObject();
		ob.element("maxdate", dateVal);// 添加首次加载时的最大日期
		JSONObject temp = new JSONObject();
		for (int i = 0; i < list.size(); i++) {
			cityVo = list.get(i);
			String type = (String) ReflectUtil.getVal(cityVo, "user_type");
			for (String f : fields) {
				int tempdata = (((Number) ReflectUtil.getVal(cityVo, f)).intValue() * 100)
						/ ((Number) ReflectUtil.getVal(cityVo, "user_num")).intValue();
				temp.element(f, tempdata);
			}
			ob.element(type, temp);
			temp.clear();
		}
		return ob.toString();
	}

	/**
	 * 获取余额消费散点图数据[[][]]
	 */
	public static String getCustomerNumJsonForCostbalance(String dateGroup, String dateVal, String businessName, String customersource, String daytype) {
		CustomerCostBalance cityVo = null;
		String TypeField[] = { "add", "flow", "keep" };
		if ("".equals(dateVal)) {
			dateVal = icmCustomerCostBalanceService.getMaxDate();
		}
		// 查询条件,获得周数或者月数
		String condition1 = String.format(paten_geneal_Cusbalance, dateVal, dateVal, businessName);
		// 查询结果
		List<CustomerCostBalance> listresult = icmCustomerCostBalanceService.getListByConditon(null, condition1, null);
		if (listresult.size() > 0) {
			cityVo = listresult.get(0);
		}
		String daytypenum = (String) ReflectUtil.getVal(cityVo, dateGroup);
		String cols = IncomeCustomerConst.getCustomerProForCostbalance();
		String condition = IncomeCustomerConst.getCustomerProForCostbalance(dateGroup, daytypenum, businessName, customersource, daytype);
		List<CustomerCostBalance> list = icmCustomerCostBalanceService.getListByConditon(cols, condition, dateGroup);
		JSONObject ob = new JSONObject();
		ob.element("maxdate", dateVal);// 添加首次加载时的最大日期
		HashMap<String, List<String[]>> dataMap = new HashMap<String, List<String[]>>();
		for (int i = 0; i < list.size(); i++) {
			cityVo = list.get(i);
			String type = (String) ReflectUtil.getVal(cityVo, "user_type");
			String[] num = { "", "",""};
			num[0] = ((Number) ReflectUtil.getVal(cityVo, "cost")).toString();
			num[1] = ((Number) ReflectUtil.getVal(cityVo, "balance")).toString();
			num[2] = (String) ReflectUtil.getVal(cityVo, "user_id");
			List<String[]> curList = dataMap.get(type);
			if (curList == null) {
				curList = new ArrayList<String[]>();
			}
			curList.add(num);
			dataMap.put(type, curList);
		}
		for (String f : TypeField) {
			ob.element(f, dataMap.get(f));
		}
		return ob.toString();
	}

	/**
	 * 获取现金余额与消耗周期散点图数据[[][]]
	 */
	public static String getCustomerNumJsonForCashcircle(String dateGroup, String dateVal, String businessName, String daytype) {
		CustomerCashcircle cityVo = null;
		String TypeField[] = { "0", "1" };
		if ("".equals(dateVal)) {
			dateVal = icmCustomerCashcircle.getMaxDate();
		}
		// 查询条件,获得周数或者月数
		String condition1 = String.format(paten_geneal_Cashcircle, dateVal, dateVal, businessName);
		// 查询结果
		List<CustomerCashcircle> listresult = icmCustomerCashcircle.getListByConditon(null, condition1, null);
		if(listresult.size()>0)
		{
			cityVo = listresult.get(0);
		}
		String daytypenum = (String) ReflectUtil.getVal(cityVo, dateGroup);
		String cols = IncomeCustomerConst.getCustomerProForCashCircle();// 条件相同，借用就行
		String condition = IncomeCustomerConst.getCustomerProForCashcircle(dateGroup, daytypenum, businessName, daytype);
		List<CustomerCashcircle> list = icmCustomerCashcircle.getListByConditon(cols, condition, dateGroup);
		JSONObject ob = new JSONObject();
		ob.element("maxdate", dateVal);// 添加首次加载时的最大日期
		HashMap<String, List<String[]>> dataMap = new HashMap<String, List<String[]>>();
		for (int i = 0; i < list.size(); i++) {
			cityVo = list.get(i);
			String type = (String) ReflectUtil.getVal(cityVo, "user_type");
			String[] num = { "", "",""};
			num[0] = ((Number) ReflectUtil.getVal(cityVo, "consume_cycle")).toString();
			num[1] = ((Number) ReflectUtil.getVal(cityVo, "cash_balance")).toString();
			num[2] = (String) ReflectUtil.getVal(cityVo, "user_id");
			List<String[]> curList = dataMap.get(type);
			if (curList == null) {
				curList = new ArrayList<String[]>();
			}
			curList.add(num);
			dataMap.put(type, curList);
		}
		for (String f : TypeField) {
			ob.element(f, dataMap.get(f));
		}
		return ob.toString();
	}

	/**
	 * 获取余额消费散点图数据[[][]]
	 */
	public static String getCustomerNumJsonForCosttransfer(String dateGroup, String dateVal, String businessName, String customersource,
			String daytype) {
		CustomerCostTransfer cityVo = null;
		String TypeField[] = { "tojingzhun", "flow", "tozhiding" };
		if ("".equals(dateVal)) {
			dateVal = icmCustomerCostTransferService.getMaxDate();
		}
		// 查询条件,获得周数或者月数
		String condition1 = String.format(paten_geneal_Custransfer, dateVal, dateVal, businessName);
		// 查询结果
		List<CustomerCostTransfer> listresult = icmCustomerCostTransferService.getListByConditon(null, condition1, null);
		if (listresult.size() > 0) {
			cityVo = listresult.get(0);
		}
		String daytypenum = (String) ReflectUtil.getVal(cityVo, dateGroup);
		String cols = IncomeCustomerConst.getCustomerProForCostbalance();// 条件相同，借用就行
		String condition = IncomeCustomerConst.getCustomerProForCostbalance(dateGroup, daytypenum, businessName, customersource, daytype);
		List<CustomerCostTransfer> list = icmCustomerCostTransferService.getListByConditon(cols, condition, dateGroup);
		JSONObject ob = new JSONObject();
		ob.element("maxdate", dateVal);// 添加首次加载时的最大日期
		HashMap<String, List<String[]>> dataMap = new HashMap<String, List<String[]>>();
		for (int i = 0; i < list.size(); i++) {
			cityVo = list.get(i);
			String type = (String) ReflectUtil.getVal(cityVo, "user_type");
			String[] num = { "", "",""};
			num[0] = ((Number) ReflectUtil.getVal(cityVo, "cost")).toString();
			num[1] = ((Number) ReflectUtil.getVal(cityVo, "balance")).toString();
			num[2] = (String) ReflectUtil.getVal(cityVo, "user_id");
			List<String[]> curList = dataMap.get(type);
			if (curList == null) {
				curList = new ArrayList<String[]>();
			}
			curList.add(num);
			dataMap.put(type, curList);
		}
		for (String f : TypeField) {
			ob.element(f, dataMap.get(f));
		}
		return ob.toString();
	}

	/**
	 * 获取新增、流失客户等数据
	 */
	public static String getCustomerNumJsonForNew(String businessName, String customersource, String daytype) {
		String fields[] = { "user_num", "user_num_chain", "arpu", "arpu_chain" };
		String cols = IncomeCustomerConst.getCustomerProForNew();
		String condition = IncomeCustomerConst.getCustomerProForNew(businessName, daytype, customersource);
		List<CustomerNewAdd> list = icmCustomerNewService.getListByConditon(cols, condition, null);
		JSONObject ob = new JSONObject();
		CustomerNewAdd cityVo = null;
		TreeMap<String, List<CustomerNewAdd>> stat_dateMap = new TreeMap<String, List<CustomerNewAdd>>(ReflectUtil.groupByField(list, "stat_date"));
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>();
		for (String date : stat_dateMap.keySet()) {// 按日期获得数据
			list = stat_dateMap.get(date);
			for (int i = 0; i < list.size(); i++) {
				cityVo = list.get(i);
				String type = (String) ReflectUtil.getVal(cityVo, "user_type");
				for (String f : fields) {
					List<Number> curList = dataMap.get(type + "#" + f);
					if (curList == null) {
						curList = new ArrayList<Number>();
					}
					curList.add((Number) ReflectUtil.getVal(cityVo, f));
					dataMap.put(type + "#" + f, curList);
				}
			}
		}
		ob.element("time", stat_dateMap.keySet());
		for (String p : dataMap.keySet()) {
			ob.element(p, dataMap.get(p));
		}
		return ob.toString();
	}

	/**
	 * 获取展现消费客户数趋势图数据
	 */
	public static String getCustomerNumJsonForDispcost(String dateGroup, String stat_type, String businessName, String customerType) {
		String fields[] = { "user_num", "user_num_chain", "arpu", "arpu_chain" };

		String cols = IncomeCustomerConst.getCustomerProForDispcost(dateGroup);
		String condition = IncomeCustomerConst.getCustomerProForDispcost(stat_type, businessName, customerType);
		List<CustomerDispcost> list = icmCustomerDispcostService.getListByConditon(cols, condition, dateGroup);
		TreeMap<String, List<CustomerDispcost>> stat_dateMap = new TreeMap<String, List<CustomerDispcost>>(ReflectUtil.groupByField(list, dateGroup));

		JSONObject ob = new JSONObject();
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for (int i = 0; i < 2; i++) {
				CustomerDispcost cityVo = stat_dateMap.get(date).get(i);
				String type = (String) ReflectUtil.getVal(cityVo, "user_type");
				for (String f : fields) {
					List<Number> curList = dataMap.get(type + "#" + f);
					if (curList == null) {
						curList = new ArrayList<Number>();
					}
					curList.add((Number) ReflectUtil.getVal(cityVo, f));//
					dataMap.put(type + "#" + f, curList);// 添加user_type类型
				}
			}
		}
		ob.element("time", stat_dateMap.keySet());

		for (String p : dataMap.keySet()) {
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}

	/**
	 * 获取智能置顶转换客户图数据
	 */
	public static String getCustomerNumJsonForTransfer(String dateGroup, String stat_type, String businessName, String customerType) {
		String fields[] = { "user_num", "user_num_chain", "user_num_per", "user_num_per_chain" };
		String cols = IncomeCustomerConst.getCustomerProForTransfer();
		String condition = IncomeCustomerConst.getCustomerProForTransfer(stat_type, businessName, customerType);
		List<CustomerTransfer> list = icmCustomerTransferService.getListByConditon(cols, condition, dateGroup);
		TreeMap<String, List<CustomerTransfer>> stat_dateMap = new TreeMap<String, List<CustomerTransfer>>(
				ReflectUtil.groupByField(list, "stat_date"));

		JSONObject ob = new JSONObject();
		HashMap<String, List<Number>> dataMap = new HashMap<String, List<Number>>(); // stat_date : [dataList]
		for (String date : stat_dateMap.keySet()) {
			for (int i = 0; i < 3; i++) {
				CustomerTransfer cityVo = stat_dateMap.get(date).get(i);
				String type = (String) ReflectUtil.getVal(cityVo, "user_type");
				for (String f : fields) {
					List<Number> curList = dataMap.get(type + "#" + f);
					if (curList == null) {
						curList = new ArrayList<Number>();
					}
					curList.add((Number) ReflectUtil.getVal(cityVo, f));// per类型数据全为0？ 不是number类型
					dataMap.put(type + "#" + f, curList);// 添加user_type类型
				}
			}
		}
		ob.element("time", stat_dateMap.keySet());

		for (String p : dataMap.keySet()) {
			ob.element(p, dataMap.get(p));
		}

		return ob.toString();
	}

	/**
	 * echarts点击获取客户开通人数(精准，置顶，智能) 表格展示数据
	 */
	public static String getCustomerProTableJson(String dateGroup, String stat_type, String businessName, String dateVal) {
		if ("week_num".equals(dateGroup)) {
			dateGroup = "stat_date";
		}
		String condition = IncomeCustomerConst.getCustomerProTableCon(dateGroup, stat_type, businessName, dateVal);
		List<CustomerBalances> list = icmCustomerService.getListByConditon(null, condition, "user_num desc");

		JSONObject ob = new JSONObject();
		ob.put("dataList", list);

		return ob.toString();
	}

	/**
	 * echarts点击获取展现消费客户人数表格展示数据
	 */
	public static String getCustomerTableJsonForDispcost(String dateGroup, String stat_type, String businessName, String dateVal) {
		String condition = IncomeCustomerConst.getCustomerDispTableCon(dateGroup, stat_type, businessName, dateVal);
		List<CustomerDispcost> list = icmCustomerDispcostService.getListByConditon(null, condition, "product_type desc");
		JSONObject ob = new JSONObject();
		ob.put("dataList", list);
		return ob.toString();
	}

	/**
	 * echarts点击获取智能置顶转换数表格展示数据
	 */
	public static String getCustomerTableJsonForTransfer(String dateGroup, String stat_type, String businessName, String dateVal) {
		String condition = IncomeCustomerConst.getCustomerTransferTableCon(stat_type, businessName, dateVal);
		List<CustomerTransfer> list = icmCustomerTransferService.getListByConditon(null, condition, "product_type desc");
		JSONObject ob = new JSONObject();
		ob.put("dataList", list);
		return ob.toString();
	}

	/**
	 * echarts点击获取新增客户人数(精准，置顶，智能) 表格展示数据
	 */
	public static String getCustomerProTableJsonForNew(String businessName, String dateVal, String datetype) {
		String condition = IncomeCustomerConst.getCustomerProTableConForNew(businessName, dateVal, datetype);
		List<CustomerNewAdd> list = icmCustomerNewService.getListByConditon(null, condition, "product_type desc");
		JSONObject ob = new JSONObject();
		ob.put("dataList", list);
		return ob.toString();
	}

}
